from functools import wraps


def ValueGetKey(dicts: dict, value) -> set:
    """
    字典中 值获取键
    :param dicts:
    :param value:
    :return:
    """
    result = set()
    for k, v in dicts.items():
        if v == value:
            result.add(k)
    return result


def ValueListGetKeyList(dicts: dict, valuelist: list) -> list:
    """
    通过字典，列表值获取列表键
    :param dicts:
    :param value:
    :return:
    """
    list_temp = []
    for value in valuelist:
        feildkeys = ValueGetKey(dicts, value)
        for fieldskey in feildkeys:
            list_temp.append(fieldskey)
    return list_temp


def DealParticiple(dicts: dict, value: str) -> str:
    """
    被分词的es键，要进行模糊或者精确查询时要使用 title_alt.keyword，及 feilds后加keyword
    :param dicts:
    :param value:
    :return:
    """
    if value in dicts.keys():
        try:
            if dicts[value]['type'] == 'keyword':
                return value
            else:
                if 'keyword' in dicts[value]['fields'].keys():
                    return value + ".keyword"
        except:
            return value
    else:
        return value


def mzfill(s):
    """

    :param s: 需要补0的字符串或数字
    :return:
    """
    if isinstance(s, int):
        s = '%05d' % s
        return s
    elif isinstance(s, str):
        s = s.zfill(5)
        return s
    else:
        return Exception("传入类型不在转换类型之内")


from line_profiler import LineProfiler


# 查询接口中每行代码执行的时间
def func_line_time(f):
    @wraps(f)
    def decorator(*args, **kwargs):
        func_return = f(*args, **kwargs)
        lp = LineProfiler()
        lp_wrap = lp(f)
        lp_wrap(*args, **kwargs)
        lp.print_stats()
        return func_return

    return decorator
